package com.lc.aiagent.controller;

import com.lc.aiagent.model.entity.ActorFilms;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController("chatClient")

@Slf4j
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping("/chatResponse")
    public ChatResponse chatResponseResult(String input) {
        ChatResponse chatResponse = this.chatClient.prompt()
                .user(input)
                .call()
                .chatResponse();

        return chatResponse;
    }

    @GetMapping("/chatEntity")
    public ActorFilms chatEntityResult(String input) {
        ActorFilms result = this.chatClient.prompt()
                .user(input)
                .call()
                .entity(ActorFilms.class);

        return result;
    }

    @GetMapping("/chatStream")
    public Flux<String> chatStreamResult(String input) {
        Flux<String> result = this.chatClient.prompt()
                .user(input)
                .stream().content();
        log.info("result:{}", result);

        return result;
    }

        @GetMapping("/ai/simple")
        public Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
            return Map.of("completion", chatClient.prompt().user(message).call().content());
        }
}